package com.huashi.dealer.modular.controller.store.role;

import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.role.param.StoreAccessVo;
import com.huashi.dealer.modular.business.role.param.StoreRoleDetailVo;
import com.huashi.dealer.modular.business.role.param.StoreRoleListParam;
import com.huashi.dealer.modular.business.role.param.StoreRoleParam;
import com.huashi.dealer.modular.business.role.param.StoreRoleVo;
import com.huashi.dealer.modular.business.role.service.StoreRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家角色控制器
 *
 * @author 谭陈强
 * @since 2025-01-27
 */
@Tag(name = "商家角色控制器")
@RestController
@RequestMapping("/store/role")
@Validated
@Slf4j
public class StoreRoleController {

    @Resource
    private StoreRoleService storeRoleService;

    /**
     * 角色列表
     */
    @GetMapping("/index")
    @Operation(summary = "角色列表")
    public CommonResult<List<StoreRoleVo>> index(StoreRoleListParam param) {
        try {
            List<StoreRoleVo> list = storeRoleService.getRoleList(param);
            return CommonResult.data(list);
        } catch (Exception e) {
            return CommonResult.error("获取角色列表失败：" + e.getMessage());
        }
    }

    /**
     * 添加角色 - 显示表单
     */
    @GetMapping("/add")
    @Operation(summary = "添加角色页面")
    public CommonResult<StoreRoleDetailVo> add() {
        try {
            StoreRoleDetailVo detail = new StoreRoleDetailVo();
            // 获取角色列表（用于上级角色选择）
            StoreRoleListParam param = new StoreRoleListParam();
            List<StoreRoleVo> roleList = storeRoleService.getRoleList(param);
            // 获取权限列表（树形结构）
            List<StoreAccessVo> accessList = storeRoleService.getAccessList();

            detail.setAccessList(accessList);
            detail.setRoleList(roleList);

            return CommonResult.data(detail);
        } catch (Exception e) {
            return CommonResult.error("获取添加角色页面数据失败：" + e.getMessage());
        }
    }

    /**
     * 添加角色 - 提交数据
     */
    @PostMapping("/add")
    @Operation(summary = "添加角色")
    public CommonResult<String> add(@Valid @RequestBody StoreRoleParam param) {
        try {
            boolean result = storeRoleService.addRole(param);
            if (result) {
                return CommonResult.ok("添加成功");
            } else {
                return CommonResult.error("添加失败");
            }
        } catch (Exception e) {
            return CommonResult.error("添加失败：" + e.getMessage());
        }
    }

    /**
     * 编辑角色 - 显示表单
     */
    @GetMapping("/edit/{roleId}")
    @Operation(summary = "编辑角色页面")
    public CommonResult<StoreRoleDetailVo> edit(@PathVariable Long roleId) {
        try {
            StoreRoleDetailVo detail = storeRoleService.getRoleDetail(roleId);
            return CommonResult.data(detail);
        } catch (Exception e) {
            return CommonResult.error("获取编辑角色页面数据失败：" + e.getMessage());
        }
    }

    /**
     * 编辑角色 - 提交数据
     */
    @PostMapping("/edit")
    @Operation(summary = "编辑角色")
    public CommonResult<String> edit(@Valid @RequestBody StoreRoleParam param) {
        try {
            boolean result = storeRoleService.updateRole(param.getRoleId(), param);
            if (result) {
                return CommonResult.ok("更新成功");
            } else {
                return CommonResult.error("更新失败");
            }
        } catch (Exception e) {
            return CommonResult.error("更新失败：" + e.getMessage());
        }
    }

    /**
     * 删除角色
     */
    @GetMapping("/delete/{roleId}")
    @Operation(summary = "删除角色")
    public CommonResult<String> delete(@PathVariable Long roleId) {
        try {
            boolean result = storeRoleService.deleteRole(roleId);
            if (result) {
                return CommonResult.ok("删除成功");
            } else {
                return CommonResult.error("删除失败");
            }
        } catch (Exception e) {
            return CommonResult.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 获取权限树形数据（JSON格式，用于前端jsTree组件）
     */
    @GetMapping("/access/tree")
    @Operation(summary = "获取权限树形数据")
    public CommonResult<String> getAccessTree(@RequestParam(required = false) Long roleId) {
        try {
            String treeJson = storeRoleService.getAccessTreeJson(roleId);
            return CommonResult.data(treeJson);
        } catch (Exception e) {
            return CommonResult.error("获取权限树形数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取角色详情（用于查看）
     */
    @GetMapping("/detail/{roleId}")
    @Operation(summary = "获取角色详情")
    public CommonResult<StoreRoleVo> detail(@PathVariable Long roleId) {
        try {
            StoreRoleDetailVo detail = storeRoleService.getRoleDetail(roleId);
            return CommonResult.data(detail.getRole());
        } catch (Exception e) {
            return CommonResult.error("获取角色详情失败：" + e.getMessage());
        }
    }
}
